package com.jinmdz.fmis.common.util;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

/**
 * file工具类
 *
 * @author LiCongLu
 * @date 2019-12-16 13:33
 */
public final class FileUtil {

    /**
     * 获取文件后缀名
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-06 17:40
     */
    public static String getSuffix(String filename) {
        if (filename != null) {
            return filename.substring(filename.lastIndexOf("."));
        }
        return "";
    }

    /**
     * 创建路径及子路径
     *
     * @param rootPath   路径
     * @param childPaths 子路径
     * @return
     * @author LiCongLu
     * @date 2019-12-16 13:39
     */
    public static void mkDirs(String rootPath, String... childPaths) {
        if (rootPath == null || rootPath.length() == 0) {
            return;
        }
        File rootFile = new File(rootPath);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        if (childPaths == null && childPaths.length == 0) {
            return;
        }
        for (String childPath : childPaths) {
            if (childPath == null && childPath.length() == 0) {
                continue;
            }
            File childFile = new File(rootPath, childPath);
            if (!childFile.exists()) {
                childFile.mkdirs();
            }
        }
    }

    /**
     * 删除路径内所有路径及文件
     *
     * @param rootPath 路径
     * @return
     * @author LiCongLu
     * @date 2019-12-16 14:33
     */
    public static void deleteFiles(String rootPath) {
        deleteFiles(new File(rootPath), true);
    }

    /**
     * 删除指定路径
     *
     * @param rootPath
     * @param childPaths
     */
    public static void deleteDirs(String rootPath, String... childPaths) {
        if (rootPath == null || rootPath.length() == 0) {
            return;
        }
        if (childPaths == null || childPaths.length == 0) {
            return;
        }
        for (String childPath : childPaths) {
            if (childPath != null && childPath.length() > 0) {
                deleteFiles(new File(rootPath, childPath), true);
            }
        }
    }

    /**
     * 删除路径内所有路径及文件
     *
     * @param rootFile  路径文件实体
     * @param deleteDir 是否删除路径
     * @return
     * @author LiCongLu
     * @date 2019-12-16 14:28
     */
    public static void deleteFiles(File rootFile, boolean deleteDir) {
        if (rootFile.exists() && rootFile.isDirectory()) {
            File[] files = rootFile.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        deleteFiles(file, deleteDir);
                        if (deleteDir) {
                            file.delete();
                        }
                    }
                    if (file.isFile()) {
                        file.delete();
                    }
                }
            }
        }
    }

    /**
     * 将字符串写入一个新的文件
     *
     * @param source 字符串
     * @param file   文件实体
     * @return
     * @author LiCongLu
     * @date 2019-12-16 16:56
     */
    public static void write2NewFile(String source, File file) {
        try {
            if (file.exists()) {
                file.delete();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream out = new FileOutputStream(file, true);
            out.write(source.getBytes("utf-8"));
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 读取文件为字节
     *
     * @param filePath 文件路径
     * @return byte[]
     * @author LiCongLu
     * @date 2019-12-16 13:38
     */
    public static byte[] readBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * base64字符串转换成image图片
     * 正确保存生产图片，返回true
     * 默认返回false
     *
     * @param imgStr   图片字符串数据
     * @param fileName 文件名字
     * @return
     * @author GaoXiangLiang
     */
    public static boolean write2ImageFile(String imgStr, String fileName) {
        //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) {
            //图像数据为空
            return false;
        }
        try {
            //Base64解码
            byte[] b = Base64.decodeBase64(new String(imgStr.getBytes()));
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //生成jpeg图片
            String imgFilePath = fileName;
            //新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
